import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// import welcome from '../components/welcome.vue'
// import users from '../components/Users/users.vue'
// import rights from '../components/power/Rights.vue'
// import roles from '../components/power/Roles.vue'
// import cate from '../components/goods/cate.vue'
// import params from '../components/goods/params.vue'
// import list from '../components/goods/list.vue'
// import add from '../components/goods/add.vue'
// import order from '../components/order/order.vue'
// import report from '../components/report/report.vue'
// 懒加载并分组 
const Login = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ '../components/Login.vue')
const Home = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ '../components/Home.vue')
const welcome = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ '../components/welcome.vue')

const users = () =>
    import ( /* webpackChunkName: "Users_Rights_Roles" */ '../components/Users/users.vue')
const rights = () =>
    import ( /* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights.vue')
const roles = () =>
    import ( /* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles.vue')

const cate = () =>
    import ( /* webpackChunkName: "Cate_Params" */ '../components/goods/cate.vue')
const params = () =>
    import ( /* webpackChunkName: "Cate_Params" */ '../components/goods/params.vue')

const list = () =>
    import ( /* webpackChunkName: "GoodsList_Add" */ '../components/goods/list.vue')
const add = () =>
    import ( /* webpackChunkName: "GoodsList_Add" */ '../components/goods/add.vue')

const order = () =>
    import ( /* webpackChunkName: "Order_Report" */ '../components/order/order.vue')
const report = () =>
    import ( /* webpackChunkName: "Order_Report" */ '../components/report/report.vue')
Vue.use(VueRouter)

const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [{
                path: '/welcome',
                component: welcome,
            },
            {
                path: '/users',
                component: users,
            },
            {
                path: '/rights',
                component: rights,
            },
            {
                path: '/roles',
                component: roles,
            },
            {
                path: '/categories',
                component: cate,
            },
            {
                path: '/params',
                component: params,
            },
            {
                path: '/goods',
                component: list,
            },
            {
                path: '/goods/add',
                component: add,
            },
            {
                path: '/orders',
                component: order,
            },
            {
                path: '/reports',
                component: report,
            },

        ]
    }

]

const router = new VueRouter({
        routes
    })
    // 导航守卫
router.beforeEach((to, from, next) => {
    // next 放行
    if (to.path === '/login') return next()
        // 从sessionstorage中读取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
        // 有token 放行
    next()
})
export default router